package controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;

import pojo.Sanpham;
import dao.SanphamDAO;
import dao.ThamsochungDAO;

@ManagedBean(name="trangChuBean")

public class TrangChuBean {
	
	
	private List<Sanpham> lstSanPhamMoiNhat = new ArrayList<Sanpham>();
	private List<Sanpham> lstSanPhamBanChayNhat = new ArrayList<Sanpham>();
	
	private int soLuongHienThi = 10;
	
	public TrangChuBean()
	{
		GetSanPhamMoiNhatVaBanChayNhat();
	}
	private void GetSanPhamMoiNhatVaBanChayNhat()
	{
        SanphamDAO sanpham = new SanphamDAO();
        /*List<Sanpham> lstSanPhamMoi = sanpham.LayDanhSachSanPham(10, "ngaynhansanpham", "desc");
        List<Sanpham> lstSanPhamHot = sanpham.LayDanhSachSanPham(10, "soluongban", "desc");
        
        List<Sanpham> lstSPMoiHot = new ArrayList<Sanpham>();
        lstSPMoiHot.addAll(lstSanPhamMoi);
        lstSPMoiHot.addAll(lstSanPhamHot);*/
        setLstSanPhamMoiNhat(sanpham.LayDanhSachSanPham(getSoLuongHienThi(), "ngaynhansanpham", "desc"));
        setLstSanPhamBanChayNhat(sanpham.LayDanhSachSanPham(getSoLuongHienThi(), "soluongban", "desc"));
        
        System.out.println("Get New Complete ..!!");
	}
	
	public void changeSoLuongHienThiListener(AjaxBehaviorEvent event) {
		System.out.println("listener: called by "
				+ event.getComponent().getClass().getName());

		HtmlSelectOneMenu combobox = (HtmlSelectOneMenu) event.getComponent();
		int contentValue = (int) combobox.getValue();

		System.out.println(combobox.getId() + " -- " + contentValue);

		setSoLuongHienThi(contentValue);
		GetSanPhamMoiNhatVaBanChayNhat();
	}
	/*
	public int XacDinhSoTrang(int nSanPham)
	{
        int nSPTrenTrang = LaySoSPTrenTrang();
        int nTrang =  nSanPham / nSPTrenTrang;
        nTrang = nSanPham % nSPTrenTrang == 0 ? nTrang: nTrang + 1;
        
        return nTrang;      
	}
	
	private int LaySoSPTrenTrang()
	{
		ThamsochungDAO thamsochung = new ThamsochungDAO();
        return Integer.parseInt(thamsochung.LayGiaTriCuaThamSo(ThamsochungDAO.TenThamSo.SO_SAN_PHAM_TREN_TRANG).toString());
	}
	
	public String LaySanPhamTheoThuTuTrang()
	{        
		Map<String,String> params =  FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String thututrang = params.get("thututrang");
		
		System.out.println("thututrang: " + thututrang);
		
		Integer index = 1;
		
		if(thututrang != null)
			index = Integer.parseInt(thututrang);	
		
		int nSPTrenTrang = LaySoSPTrenTrang();
              
        List<Sanpham> lstSPMoiHot = Lay10SPMoi10SPHot();
        
        int nTrang = XacDinhSoTrang(lstSPMoiHot.size());
        int FromIndex = nSPTrenTrang * (index - 1);
        int ToIndex = index * nSPTrenTrang;
        
        if(index == nTrang)
        	ToIndex = lstSPMoiHot.size();

        System.out.println("from: " + FromIndex + " to: " + ToIndex);
        
        getLstSanPham().clear();
        getLstSanPham().addAll(lstSPMoiHot.subList(FromIndex, ToIndex));
        
        System.out.println(getLstSanPham().get(0).getGiaban());
        System.out.println(getLstSanPham().get(1).getGiaban());
        System.out.println(getLstSanPham().get(2).getGiaban());
        
        return "TrangChu";
	}*/
	
	//--------------------
	public int getSoLuongHienThi() {
		return soLuongHienThi;
	}
	public void setSoLuongHienThi(int soLuongHienThi) {
		this.soLuongHienThi = soLuongHienThi;
	}
	public List<Sanpham> getLstSanPhamBanChayNhat() {
		return lstSanPhamBanChayNhat;
	}
	public void setLstSanPhamBanChayNhat(List<Sanpham> lstSanPhamBanChayNhat) {
		this.lstSanPhamBanChayNhat = lstSanPhamBanChayNhat;
	}
	public List<Sanpham> getLstSanPhamMoiNhat() {
		return lstSanPhamMoiNhat;
	}
	public void setLstSanPhamMoiNhat(List<Sanpham> lstSanPhamMoiNhat) {
		this.lstSanPhamMoiNhat = lstSanPhamMoiNhat;
	}


	//-----------
	
}
